package com.luo.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.luo.constant.MessageConstant;
import com.luo.dao.MemberDao;
import com.luo.dao.OrderDao;
import com.luo.dao.OrderSettingDao;
import com.luo.entity.Result;
import com.luo.pojo.Member;
import com.luo.pojo.Order;
import com.luo.pojo.OrderSetting;
import com.luo.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Zoro
 * @date 2021年08月30
 */
@Service(interfaceClass = OrderService.class)
@Transactional(readOnly = true)
public class OrderServiceImpl implements OrderService {


    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    @Transactional(readOnly = false)
    public Result submit(Map map) throws Exception {
        Date orderDate = DateUtils.parseString2Date((String) map.get("orderDate"));
        OrderSetting orderSetting = orderSettingDao.getOrderSetting(orderDate);
        //1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        } else {
            //2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
            if (orderSetting.getReservations() >= orderSetting.getNumber()) {
                return new Result(false, MessageConstant.ORDER_FULL);
            }
        }
        //3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
        //获得电话号码
        String telephone = (String) map.get("telephone");
        //根据号码获取当前用户是否为会员
        Member member = memberDao.getMemberByTel(telephone);
        if (member != null) {
            //根据会员id获取order对象
            Order order = new Order(member.getId(), orderDate, null, null, Integer.parseInt((String) map.get("setmealId")));
            List<Order> orders = orderDao.getOrderList(order);
            if (orders != null && orders.size() > 0) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        } else {
            //当前没有该用户，注册用户
            member = new Member();
            member.setIdCard((String) map.get("idCard"));
            member.setName((String) map.get("name"));
            member.setRegTime(new Date());
            member.setPhoneNumber((String) map.get("telephone"));
            member.setSex((String) map.get("sex"));
            memberDao.addMember(member);
        }

        //可以进行预约，预月设置增加一个人,预约成功，更新当日的已预约人数
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.updateOrderSetting(orderSetting);

        Order order = new Order(member.getId(), orderDate, Order.ORDERTYPE_TELEPHONE, Order.ORDERSTATUS_NO, Integer.parseInt((String) map.get("setmealId")));
        orderDao.addOrder(order);


        return new Result(true, MessageConstant.ORDER_SUCCESS, order);
    }

    @Override
    public Map findById(Integer id) {
        return orderDao.findBasicById(id);
    }

    @Override
    public Integer todayOrderNumber(String today) {
        return orderDao.todayOrderNumber(today);
    }

    @Override
    public Integer todayVisitsNumber(String today) {
        return orderDao.todayVisitsNumber(today);
    }

    @Override
    public Integer startEndSelectYes(String weekMonday, String weekSunday) {
        return orderDao.startEndSelectYes(weekMonday, weekSunday);
    }

    @Override
    public Integer startEndSelectNo(String weekMonday, String weekSunday) {
        return orderDao.startEndSelectNo(weekMonday, weekSunday);
    }

    @Override
    public List<Map<String, Object>> hotSetMeal() {
        return orderDao.hotSetMeal();
    }
}
